<?php
// +----------------------------------------------------------------------
// | Description: 菜单
// +----------------------------------------------------------------------
// | Author: linchuangbin <linchuangbin@honraytech.com>
// +----------------------------------------------------------------------

namespace app\common\model;

use app\common\exception\ExceptionEmun;
use app\common\exception\SystemException;
use Helper\ArrayHelper;
use app\common\model\base\BaseModel;
use app\common\traits\InstanceTrait;

class AdminMenu extends BaseModel
{
    use InstanceTrait;

    public function __construct($data = [], Model $parent = null, $table = '')
    {
        parent::__construct($data, $parent, $table);
        $this->pk = 'menu_id';
        $this->initBaseField();
    }

    /**
     * [getDataList 获取列表]
     * @linchuangbin
     * @DateTime  2017-02-10T21:07:18+0800
     * @return    [array]
     */
    public function getDataList($type = '')
    {
        $data = static::where($this->getBaseWhere())
                      ->field($this->baseField)
                      ->select();
        $data = $data->toArray();
        // 若type为tree，则返回树状结构
        if ($type == 'tree') {
            foreach ($data as $k => $v) {
                $data[$k]['check'] = false;
            }
            $ret = ArrayHelper::arrayToTree($data, 'id', 'pid', 'child', 0, true, array('pid'));
        } else {
            //父子关系排序
            $ret = [];
            $ret = ArrayHelper::arraySortChild($data, $ret);
        }
        return $ret;
    }

    /**
     * [getDataById 根据主键获取详情]
     * @linchuangbin
     * @DateTime  2017-02-10T21:16:34+0800
     * @param string $id [主键]
     * @return    [array]
     */
    public function getByID($id = '')
    {
        $data = parent::getByID($id);
        if (!$data) {
            ExceptionEmun::throwException(SystemException::INVALID_PARAMETER);
            return false;
        }
        return $data;
    }

    /**
     * [createData 新建]
     * @linchuangbin
     * @DateTime  2017-02-10T21:19:06+0800
     * @param array $param [description]
     * @return bool [array]                         [description]
     * @throws Exception
     */
    public function createData($param)
    {
        //module,首字母大写
        $param['module'] = ucfirst($param['module']);
        static::create($param);
        return true;
    }


}
